/**
 * Copyright &copy; 2016-2022 liuhangjun All rights reserved.
 */
package com.channelsharing.hongqu.supplier.api.service.impl;

import com.channelsharing.common.cache.CacheDuration;
import com.channelsharing.common.cache.ExpireTimeConstant;
import com.channelsharing.common.entity.Paging;
import com.channelsharing.common.service.CrudServiceImpl;
import com.channelsharing.hongqu.supplier.api.constant.Constant;
import com.channelsharing.hongqu.supplier.api.dao.SecKillGoodsDao;
import com.channelsharing.hongqu.supplier.api.entity.GoodsInfo;
import com.channelsharing.hongqu.supplier.api.entity.ProductInfo;
import com.channelsharing.hongqu.supplier.api.entity.SecKillGoods;
import com.channelsharing.hongqu.supplier.api.service.GoodsDescriptionService;
import com.channelsharing.hongqu.supplier.api.service.GoodsInfoService;
import com.channelsharing.hongqu.supplier.api.service.ProductInfoService;
import com.channelsharing.hongqu.supplier.api.service.SecKillGoodsService;
import lombok.NonNull;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Service;

import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * 商品信息接口Service
 * @author liuhangjun
 * @version 2018-06-06
 */
@CacheDuration(duration = ExpireTimeConstant.ONE_DAY)
@Service
public class SecKillGoodsServiceImpl extends CrudServiceImpl<SecKillGoodsDao, SecKillGoods> implements SecKillGoodsService {
    
    @Autowired
    private GoodsDescriptionService goodsDescriptionService;
    
    @Autowired
    private ProductInfoService productInfoService;
    
    /**
     * @param id
     * @return
     */
    @Override
    public SecKillGoods findOne(Long id){
        SecKillGoods entity = new SecKillGoods();
        entity.setId(id);
        SecKillGoods secKillGoods = super.findOne(entity);
        if(secKillGoods == null){
            return new SecKillGoods();
        }
        ProductInfo productInfo = productInfoService.findOne(secKillGoods.getProductId());
        secKillGoods.setProductInfo(productInfo);
        return secKillGoods;
    }
    
    /**
     * 查询单个商品时，加上详情内容
     * @param entity
     * @return
     */
    @Override
    public SecKillGoods findOne(@NotNull SecKillGoods entity){
        SecKillGoods secKillGoods = super.findOne(entity);
        return secKillGoods;
    }

    @Override
    public Paging<SecKillGoods> findPaging(@NonNull SecKillGoods entity) {
        Paging<SecKillGoods> paging = new Paging<>();
        paging.setCount(dao.findAllCount(entity));
        List<SecKillGoods> secKillGoodsList = super.dao.findList(entity);
        for (SecKillGoods secKillGoods : secKillGoodsList) {
            secKillGoods.setProductInfo(productInfoService.findOne(secKillGoods.getProductId()));
        }
        paging.setRows(secKillGoodsList);
        return paging;
    }

}
